package com.douqu.game.core.entity.sprite;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.entity.goods.GoodsProp;
import com.douqu.game.core.factory.DataFactory;
import io.netty.buffer.ByteBuf;

/**
 * Created by bean on 2017/7/26.
 */
public class Card extends Sprite {


    /**
     * 品质
     * 1.普通(白)
     * 2.稀有(蓝)
     * 3.史诗(紫)
     * 4.传说(橙)
     */
    public int quality;

    /** 类型
     * 1.英雄
     * 2.兵种
     * */
    public int type;



    /** 消耗水晶 */
    public int crystal;

    /** 产地描述 */
    public String sourceRemark;

    /** 解锁条件 */
    public String unlockRemark;

    /** 升级所需碎片 */
    public int lvUpProp;

    /**
     * 召唤的兵种ID
     */
    public int soldierId;
    /**
     * 召唤的兵种数量
     */
    public int soldierCount;

    /**
     * 上阵卡组
     */
    public int battleIndex;

    public Card()
    {
        super();
    }



    @Override
    public void copyTo(GameObject gameObject)
    {
        super.copyTo(gameObject);

        Card card = (Card) gameObject;
        card.quality = this.quality;
        card.type = this.type;
        card.crystal = this.crystal;
        card.sourceRemark = this.sourceRemark;
        card.unlockRemark = this.unlockRemark;
        card.lvUpProp = this.lvUpProp;
        card.soldierId = this.soldierId;
        card.soldierCount = this.soldierCount;
        card.battleIndex = this.battleIndex;
    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        super.writeTo(buffer);

        buffer.writeByte(battleIndex);
    }

    @Override
    public void loadFrom(ByteBuffer buffer)
    {
        super.loadFrom(buffer);

        battleIndex = buffer.readByte();
    }




    @Override
    public void setVariable(String key, String value)
    {
        if("soldierId".equals(key))
        {
            soldierId = Integer.parseInt(value);
            Soldier soldier = DataFactory.getInstance().getGameObject(DataFactory.SOLDIER_KEY, soldierId);
            if(soldier == null)
            {
                System.out.println("Load Card Error: Soldier is null! soldierId=" + soldierId);
            }
        }
        else if("soldierCount".equals(key))
        {
            soldierCount = Integer.parseInt(value);
            if(soldierCount <= 0)
            {
                System.out.println("Load Card Error: soldierCount=" + soldierCount);
            }
        }
        else if("lvUpProp".equals(key))
        {
            lvUpProp = Integer.parseInt(value);
            GoodsProp prop = DataFactory.getInstance().getGameObject(DataFactory.GOODS_PROP_KEY, lvUpProp);
            if(prop == null)
            {
                System.out.println("Load Card Error: GoodsProp is null! lvUpProp=" + lvUpProp);
            }
        }
        else
            super.setVariable(key, value);
    }
}
